package com.pp.tourism.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pp.tourism.web.exception.SubmitException;
import com.pp.tourism.web.pojo.User;
import com.pp.tourism.web.service.LoginService;
import com.pp.tourism.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.pp.tourism.web.exception.ApiRtnCode.LOGIN_INFO_INCORRECT;
import static com.pp.tourism.web.exception.ApiRtnCode.LOGIN_INFO_NOT_NULL;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserService userService;

    @Override
    public User login(String username, String password) {
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            throw new SubmitException("username or password not be null",LOGIN_INFO_NOT_NULL);
        }
        User user = userService.selectOne(new EntityWrapper<User>().eq("username", username).eq("password", password));
        if(StringUtils.isEmpty(user)){
          throw new SubmitException("username or password is not correct",LOGIN_INFO_INCORRECT);
        }
        return user;
    }
}
